package org.jeecg.util.encryption;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AesNewUtil {


        private static String iv = "0123456789ABCDEF";//偏移量字符串必须是16位 当模式是CBC的时候必须设置偏移量
        private static String Algorithm = "AES";
        private static String AlgorithmProvider = "AES/CBC/PKCS5Padding"; //算法/模式/补码方式

        public static byte[] generatorKey() throws NoSuchAlgorithmException {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(Algorithm);
            keyGenerator.init(256);//默认128，获得无政策权限后可为192或256
            SecretKey secretKey = keyGenerator.generateKey();
            return secretKey.getEncoded();
        }

        public static IvParameterSpec getIv() throws UnsupportedEncodingException {
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes("utf-8"));
            return ivParameterSpec;
        }

        /**
         * 加密
         * @param src
         * @param key
         * @return
         * @throws NoSuchAlgorithmException
         * @throws NoSuchPaddingException
         * @throws InvalidKeyException
         * @throws IllegalBlockSizeException
         * @throws BadPaddingException
         * @throws UnsupportedEncodingException
         * @throws InvalidAlgorithmParameterException
         */
        public static byte[] encrypt(String src, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException,
                InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
            SecretKey secretKey = new SecretKeySpec(key, Algorithm);
            IvParameterSpec ivParameterSpec = getIv();
            Cipher cipher = Cipher.getInstance(AlgorithmProvider);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
            byte[] cipherBytes = cipher.doFinal(src.getBytes(Charset.forName("utf-8")));
            return cipherBytes;
        }

        /**
         * 解密
         * @param src 原密码
         * @param key 解密后的byte数组
         * @return
         * @throws Exception
         */
        public static byte[] decrypt(String src, byte[] key) throws Exception {
            SecretKey secretKey = new SecretKeySpec(key, Algorithm);

            IvParameterSpec ivParameterSpec = getIv();
            Cipher cipher = Cipher.getInstance(AlgorithmProvider);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] hexBytes = hexStringToBytes(src);
            byte[] plainBytes = cipher.doFinal(hexBytes);
            return plainBytes;
        }

        /**
         * 将byte转换为16进制字符串
         * @param src
         * @return
         */
        public static String byteToHexString(byte[] src) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < src.length; i++) {
                int v = src[i] & 0xff;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    sb.append("0");
                }
                sb.append(hv);
            }
            return sb.toString();
        }

        /**
         * 将16进制字符串装换为byte数组
         * @param hexString
         * @return
         */
        public static byte[] hexStringToBytes(String hexString) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] b = new byte[length];
            for (int i = 0; i < length; i++) {
                int pos = i * 2;
                b[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }
            return b;
        }

        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
        }

        /**
         * 直接传入psd然后解码进行验证使用
         * @param psd 前台传过来的String 加密后的数组
         * @return
         */
        public static String decrypt(String psd) {
            try {
                byte key[] = "1234567890ABCDEF1234567890ABCDEf".getBytes("utf-8");
                return new String(decrypt(psd, key));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }


        }

    public static void main(String[] args) throws Exception {
//        byte key[] = "1234567890ABCDEF1234567890ABCDEf".getBytes("utf-8");
//        String password = "1111";
//        String en = AesNewUtil.encrypt(password,"1234567890ABCDEF1234567890ABCDEf".getBytes("utf-8")).toString();
//        System.out.println("加密后："+en);
        String password = AesNewUtil.decrypt("20df6d079a1c2b0d6082bf775337f5e4");
        System.out.println("解密后:"+password);
    }

    }
